﻿using SIPSorcery.SIP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Agent
{
    public partial class Agent : Form
    {
        public Agent()
        {
            InitializeComponent();
        }

        private void Agent_Load(object sender, EventArgs e)
        {
            Vars.Init();
            label1.Text += Vars.sipUser;
            label2.Text = "状态：Free";
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            listBox1.Items.Clear();
            Thread.Sleep(1000);
            Vars.sip.SendMsg("0", "Register:Number=" + Vars.sipUser + ";");
        }

        public void ring()
        {
            label2.Text = "状态：Ringing";
            label3.Text = "通话对方号码:"+ Vars.sip?.GetRemoteUser();
            button3.Enabled = true;
        }
        public void talk()
        {
            label2.Text = "状态：Talking";
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = false;
        }
        public void end()
        {
            label2.Text = "状态：Free";
            label3.Text = "通话对方号码:";
            label4.Text = "报警辅助信息:";
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            listBox1.Items.Clear();
            Vars.sip.SendMsg("0", "Free:Number=" + Vars.sipUser + ";LastMessage="+Vars.message+"\n"+Vars.chatMessage+"EndLastMessage");
            Vars.message = "";
            Vars.chatMessage = "";
        }
        public void getMsgFromInvite(string msg)
        {
            Vars.message = msg;
            label4.Text = "报警辅助信息:" + msg;
        }

        public void receiveMsg(SIPRequest sipRequest)
        {
            if (sipRequest.Body.Contains("RealTimeMessage="))
            {
                string realTimeMessage = sipRequest.Body.Split("RealTimeMessage=")[1].Split(";")[0];
                Vars.message = realTimeMessage;
                label4.Text = "报警辅助信息:" + realTimeMessage;
            }
            else if (sipRequest.Body.Contains("Chat="))
            {
                string chat = sipRequest.Body.Split("Chat=")[1].Split(";")[0];
                Vars.chatMessage += "对方:" + chat + "\n";
                listBox1.Items.Add("对方:" + chat);
            }
            else if (sipRequest.Body.Contains("LastMessage=")) { 
                string lastMessage = sipRequest.Body.Split("LastMessage=")[1].Split("EndLastMessage")[0];
                Vars.chatMessage = lastMessage;
                foreach (string item in lastMessage.Split('\n'))
                {
                    listBox1.Items.Add(item);
                }
            }
        }

        private void Agent_FormClosing(object sender, FormClosingEventArgs e)
        {
            Vars.sip.SendMsg("0", "UnRegister:Number=" + Vars.sipUser + ";");
            Thread.Sleep(1000);
            Vars.sip?.CleanUp();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Vars.sip.UserOptionSelect("Accept");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Vars.sip.UserOptionSelect("Bye");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Vars.sip.SendMsg("0", "Out:Number=" + Vars.sipUser + ";" + "flag:Chat=" + textBox1.Text + ";");
            Vars.chatMessage += "我:" + textBox1.Text + "\n";
            listBox1.Items.Add("我:" + textBox1.Text);
            textBox1.Text = "";
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
